package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.ruoyi.system.domain.StorySysEmployee;
import com.ruoyi.system.mapper.StorySysEmployeeMapper;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private StorySysEmployeeMapper storySysEmployeeMapper;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
{
    System.out.println("执行有参方法：" + params);
}

    public void ryNoParams()
    {
        List<StorySysEmployee> storySysEmployees = storySysEmployeeMapper.selectListByOpinionIsNull();
        for (StorySysEmployee storySysEmployee : storySysEmployees) {
            String opinionTime = storySysEmployee.getOpinionTime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date date1 = null;
            try {
                date1 = sdf.parse(opinionTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
//            0 0 9 * * ?

            Date date2 = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            long timeInMillis1 = calendar.getTimeInMillis();
            calendar.setTime(date2);
            long timeInMillis2 = calendar.getTimeInMillis();

            long betweenDays =  (timeInMillis2 - timeInMillis1) / (1000L*3600L*24L);
            System.out.println("betweenDays = " + betweenDays);
            if (betweenDays >= 3){
//                doPostTestFour("100004044");
//                System.out.println("betweenDays = " + betweenDays);
                Map<String, String> paramMap = new HashMap();
                paramMap.put("from","900000010");
                paramMap.put("client","GZ000007");
                paramMap.put("clientName","厦工小事");
                paramMap.put("type","86");
                paramMap.put("title","厦工小事");
                paramMap.put("sub","你有厦工小事待处理，请前往查询");
                paramMap.put("fromName","厦工小事");
//                storySysEmployee.getSysEmployeeId()
                paramMap.put("to", "100004044");
                String result = doPost("http://fcim.xit.edu.cn:82/public/officialAccounts", paramMap);
                System.out.println("result = " + result);
            }
        }
    }


    public void send(int id) {
        Map<String, String> paramMap = new HashMap();
        paramMap.put("from","900000010");
        paramMap.put("client","GZ000007");
        paramMap.put("clientName","厦工小事");
        paramMap.put("type","86");
        paramMap.put("title","小事说");
        paramMap.put("sub","您有小事说信息待阅，请前往我的小事说对话框查阅");
//        paramMap.put("sub","您有一条待处理小事，请在前往处理的小事里查阅");
        paramMap.put("fromName","厦工小事");
//                storySysEmployee.getSysEmployeeId()
        paramMap.put("to", "100004044");
        String result = doPost("http://fcim.xit.edu.cn:82/public/officialAccounts", paramMap);
        System.out.println("result = " + result);
    }



    public static String doPost(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(JSON.toJSONString(param),"utf-8");
                entity.setContentEncoding("UTF-8");   //解决中文乱码问题
                entity.setContentType("application/json"); //解决中文乱码问题
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }


}
